﻿<!DOCTYPE>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>服务区分析</title>
        <style type="text/css">
            body{
                margin: 0;
                overflow: hidden;
                background: #fff;
            }
            #map{
                position: relative;
                height: 510px;
                border:1px solid #3473b7;
            }
            #toolbar {
                position: relative;
                padding-top: 5px;
                padding-bottom: 10px;
            }
        </style>
        <link href='./css/bootstrap.min.css' rel='stylesheet' />
        <link href='./css/bootstrap-responsive.min.css' rel='stylesheet' />
        <script src='../libs/SuperMap.Include.js'></script>
        <script type="text/javascript">
            var  local, map, layer, vectorLayer, markerLayer, drawPoint,
            centersArray = [], weightsArray = [], n = 0,
            style = {
                strokeColor: "#304DBE",
                strokeWidth: 1,
                pointerEvents: "visiblePainted",
                fillColor: "#304DBE",
                fillOpacity: 0.4
            },
            host = document.location.toString().match(/file:\/\//)?"http://localhost:8090":'http://' + document.location.host,
            url1=host+"/iserver/services/map-changchun/rest/maps/长春市区图",
            url2=host+"/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
            function init(){
                vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
                drawPoint = new SuperMap.Control.DrawFeature(vectorLayer, SuperMap.Handler.Point);
                drawPoint.events.on({"featureadded": drawCompleted});
                map = new SuperMap.Map("map",{controls: [
                    new SuperMap.Control.LayerSwitcher(),
                    new SuperMap.Control.ScaleLine(),
                    new SuperMap.Control.Zoom(),
                    new SuperMap.Control.Navigation({
                        dragPanOptions: {
                            enableKinetic: true
                        }}),
                    drawPoint], units: "m"
                });
                layer = new SuperMap.Layer.TiledDynamicRESTLayer("Changchun", url1, {transparent: true, cacheEnabled: true}, {maxResolution:"auto"});
                layer.events.on({"layerInitialized":addLayer});
                vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
                markerLayer = new SuperMap.Layer.Markers("Markers");
            }

            function addLayer() {

                map.addLayers([layer, vectorLayer, markerLayer]);
                map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 1);
            }
            function selectCenters(){
                clearElements();
                drawPoint.activate();
            }
            function drawCompleted(drawGeometryArgs){
                var point = drawGeometryArgs.feature.geometry,
                        size = new SuperMap.Size(44, 33),
                        offset = new SuperMap.Pixel(-(size.w/2), -size.h),
                        icon = new SuperMap.Icon("../theme/images/marker.png", size, offset);
                markerLayer.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
                centersArray.push(point);
                n++;
                weightsArray.push(400+n*100);
            }
            function findServiceAreas() {
                vectorLayer.removeAllFeatures();
                drawPoint.deactivate();
                var findServiceAreasService, parameter, analystParameter, resultSetting;
                resultSetting = new SuperMap.REST.TransportationAnalystResultSetting({
                    returnEdgeFeatures: true,
                    returnEdgeGeometry: true,
                    returnEdgeIDs: true,
                    returnNodeFeatures: true,
                    returnNodeGeometry: true,
                    returnNodeIDs: true,
                    returnPathGuides: true,
                    returnRoutes: true
                });
                analystParameter = new SuperMap.REST.TransportationAnalystParameter({
                    resultSetting: resultSetting,
                    weightFieldName: "length"
                });
                parameter = new SuperMap.REST.FindServiceAreasParameters({
                    centers: centersArray,
                    isAnalyzeById: false,
                    parameter: analystParameter,
                    weights: weightsArray
                });
                findServiceAreasService  = new SuperMap.REST.FindServiceAreasService(url2, {
                    eventListeners: {"processCompleted": processCompleted}});
                findServiceAreasService.processAsync(parameter);
            }
            function processCompleted(findServiceAreasEventArgs) {
                vectorLayer.removeAllFeatures();
                var result = findServiceAreasEventArgs.result,
                        features = [];
                if (result.serviceAreaList) {
                    for (var i=0,serviceAreaList=result.serviceAreaList,len=serviceAreaList.length; i<len; i++) {
                        var feature = new SuperMap.Feature.Vector();
                        feature.geometry = serviceAreaList[i].serviceRegion;
                        feature.style = style;
                        features.push(feature);
                    }
                }
                vectorLayer.addFeatures(features);
            }
            function clearElements() {
                n = 0;
                centersArray = [];
                weightsArray = [];
                markerLayer.clearMarkers();
                vectorLayer.removeAllFeatures();
            }
        </script>
    </head>
    <body onload="init()">
        <div id="toolbar">
            <input type="button" class="btn" value="服务中心点" onclick="selectCenters()" />
            <input type="button" class="btn" value="提交" onclick="findServiceAreas()" />
            <input type="button" class="btn" value="清除" onclick="clearElements()" />
        </div>
        <div id="map"></div>
    </body>
</html>
